package com.lianniu.mgr.product.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductCateVo;
import com.lianniu.mgr.product.service.IProductCateService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/rest/productCate")
public class ProductCateRestController {
	
	private final static Logger logger = Logger.getLogger(ProductCateRestController.class);
	  
	
	  @Autowired
	  private IProductCateService productCateService;
	  
	  @RequestMapping(value="/productCateList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult productCateList(HttpServletRequest request, ProductCateVo curVo, PaginationInfo paginationInfo)
	  {
	       logger.info("ProductCateRestController  productCateList begin.");
	       try {
	           
	           logger.info("productCateList begin.");          
	           BaseResult baseResult = productCateService.selectByProductCateForPage(curVo, paginationInfo);
	           logger.info("productCateList end.");
	           logger.info("ProductCateRestController productCateList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ProductCateRestController productCateList error:" + e.getMessage());
	       logger.error("ProductCateRestController productCateList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	  
	  @RequestMapping(value = {"/productCateAdd"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult dictionaryAdd(HttpServletRequest request,ProductCateVo curVo) {
	      logger.info("ProductCateRestController productCateAdd begin.");      
	      logger.info("ProductCateRestController productCateAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("productCateAdd begin.");	          
	          BaseResult baseResult = productCateService.insertProductCate(curVo);
	          logger.info("productCateAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("productCateAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("productCateAdd error:"+e.getMessage());
	      logger.info("ProductCateRestController productCateAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  } 
	  
	  
	  @RequestMapping(value = {"/productCateEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult productCateEdit(HttpServletRequest request,ProductCateVo curVo) {
	      logger.info("ProductCateRestController productCateEdit begin.");      
	      logger.info("ProductCateRestController productCateEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("productCateEdit begin.");
	          BaseResult baseResult = productCateService.updateById(curVo);
	          logger.info("productCateEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("productCateEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("productCateEdit error:"+e.getMessage());
	      logger.info("ProductCateRestController productCateEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }  
	  
	  
	  @RequestMapping(value="/selectProductCateById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectProductCateById(HttpServletRequest request,ProductCateVo curVo) {
	        logger.info("ProductCateRestController selectProductCateById begin.");
	        try {
	                    
	          BaseResult baseResult = productCateService.selectById(curVo);
	          logger.info("selectDictionaryById end.");
	          logger.info("ProductCateRestController selectProductCateById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductCateRestController selectProductCateById error:" + e.getMessage());
	          logger.error("ProductCateRestController selectProductCateById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value="/selectParentCate",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectParentCate(HttpServletRequest request) {
	        logger.info("ProductCateRestController selectParentCate begin.");
	        try {
	                    
	          BaseResult baseResult = productCateService.selectParentCate();
	          logger.info("selectParentCate end.");
	          logger.info("ProductCateRestController selectParentCate end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductCateRestController selectParentCate error:" + e.getMessage());
	          logger.error("ProductCateRestController selectParentCate end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value="/selectChildCateKv",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectChildCateKv(HttpServletRequest request) {
	        logger.info("ProductCateRestController selectChildCateKv begin.");	        
	        String cateId = request.getParameter("cateId");
	        logger.info("ProductCateRestController selectChildCateKv cateId:" + JSON.toJSONString(cateId));
	        try {	                    
	          BaseResult baseResult = productCateService.selectChildCateKV(cateId);
	          logger.info("selectChildCateKv end.");
	          logger.info("ProductCateRestController selectChildCateKv end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductCateRestController selectChildCateKv error:" + e.getMessage());
	          logger.error("ProductCateRestController selectChildCateKv end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value = {"/productCateDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult productCateDel(HttpServletRequest request) {
	      logger.info("ProductCateRestController productCateDel begin.");
	     try {          
	          logger.info("productCateDel begin.");
	          BaseResult baseResult = productCateService.deleteProductCateBatch(request.getParameter("ids").split(","));	
	          logger.info("productCateDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("productCateDel end.");
	          
	          logger.info("ProductCateRestController productCateDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductCateRestController productCateDel error:" + e.getMessage());
	      logger.info("ProductCateRestController productCateDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	  
	  
	  @RequestMapping(value = {"/checkCateName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkCateName(HttpServletRequest request) {
	      Boolean flag=true;
	      String cateName=  request.getParameter("cateName");
	      logger.info("ProductCateRestController checkCateName begin.");
	     try {          
	          logger.info("checkCateName begin.");
	          if("".equalsIgnoreCase(cateName)||cateName==null)
	          {return flag;}
	          flag = productCateService.checkCateName(cateName);         
	          logger.info("checkCateName end.");
	          
	          logger.info("ProductCateRestController checkCateName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductCateRestController checkCateName error:" + e.getMessage());
	      logger.info("ProductCateRestController checkCateName end.");
	      return flag;
	    }

	  }
	  
	  
	  @RequestMapping(value = {"/checkCateNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkCateNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String cateName=  request.getParameter("cateName");
	      String cateId=request.getParameter("cateId");
	      logger.info("ProductCateRestController checkCateNameEdit begin.");
	     try {          
	          logger.info("checkTypeNameEdit begin.");
	          ProductCateVo vo=new ProductCateVo();	         
	          vo.setCateId(cateId);
	          
	          BaseResult baseResult =productCateService.selectById(vo);	          
	          ProductCateVo curVo=(ProductCateVo) baseResult.getData();
	          if(curVo.getCateId().equals(cateId)&&curVo.getCateName().equals(cateName))
	          {return flag;}
	          if("".equalsIgnoreCase(cateName)||cateName==null)
	          {return flag;}
	          flag = productCateService.checkCateName(cateName);         
	          logger.info("checkCateNameEdit end.");
	          
	          logger.info("ProductCateRestController checkCateNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductCateRestController checkCateNameEdit error:" + e.getMessage());
	      logger.info("ProductCateRestController checkCateNameEdit end.");
	      return flag;
	    }

	  }
	  

}
